package com.kingint.web.portal.base.service.impl;

import com.kingint.mbg.bas.entity.BasGoods;
import com.kingint.mbg.bas.mapper.BasGoodsMapper;
import com.kingint.mbg.dto.GoodsReturn;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.portal.base.service.GoodsService;
import com.kingint.web.sys.service.UmsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private UmsAdminService adminService;
    @Resource
    private BasGoodsMapper basGoodsMapper;
    @Override
    public List<GoodsReturn> getAllGoodsList() {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("当前账号没有酒店id关联");
           return null;
        }
        List<GoodsReturn> list = basGoodsMapper.getGoodsReturnList(hotelId, 0);
        if(!CollectionUtils.isEmpty(list)){
            List<GoodsReturn> collect = list.stream().filter(s -> s.getPid() == 0).collect(Collectors.toList());
            cancelGoods(collect,list);
            return collect;
        }
        return null;
    }

    @Override
    public List<GoodsReturn> getGoodsForType(Integer type){
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("当前账号没有酒店id关联");
            return null;
        }
        //获取数据
        List<GoodsReturn> list = basGoodsMapper.getGoodsReturnListForOne(hotelId, 0,type);
        if(!CollectionUtils.isEmpty(list)){

            List<GoodsReturn> collect = list.stream().filter(s -> s.getPid() == 0).collect(Collectors.toList());

           // getGoods(list);
            cancelGoods(collect,list);
            return collect;
        }
        return null;
    }
 //删除是消耗品为分类的
    private  void getGoods( List<GoodsReturn> collect){
        Iterator<GoodsReturn> iterator = collect.iterator();
        while (iterator.hasNext()){
            GoodsReturn next = iterator.next();
            if(next.getIsgoods().intValue()==1){
                //删除分类
                 collect.remove(next);
            }
        }
    }

    private void cancelGoods(List<GoodsReturn> aClass, List<GoodsReturn> allList) {
        if (!CollectionUtils.isEmpty(aClass)) {
            //为一级菜单设置子菜单
            for (GoodsReturn info : aClass) {
                getChildList(info, allList);
            }
        }
    }
    private void getChildList(GoodsReturn info, List<GoodsReturn> aClass) {
        List<GoodsReturn> serviceClass = info.getGoodsList();
        if (CollectionUtils.isEmpty(serviceClass)) {
            serviceClass = new ArrayList<>();
        }
        for (GoodsReturn ser : aClass) {
            if (ser.getPid() == info.getId()) {
                serviceClass.add(ser);
            }
        }
        //递归查询
        if (serviceClass.size() != 0) {
            for (GoodsReturn child : serviceClass) {
                getChildList(child, aClass);
            }
        }
        info.setGoodsList(serviceClass);
    }
}
